package com.love_weinuo.util.dialog;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;

import com.lzy.okgo.callback.StringCallback;
import com.love_weinuo.R;
import com.love_weinuo.bean.DrugBean;
import com.love_weinuo.databinding.DialogSelectDrugBinding;
import com.love_weinuo.ui.adapter.DrugAdapter;
import com.love_weinuo.ui.adapter.OnItemClickListener;
import com.love_weinuo.util.GsonUtil;
import com.love_weinuo.util.HTTPAPI;
import com.love_weinuo.util.LogUtil;
import com.love_weinuo.util.listener.OnDrugSelectListener;
import com.love_weinuo.util.listener.OnPindanPointListener;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2019/3/2.
 */

public class SelectDrugDialog extends BaseDialog {

    Context context;
    DialogSelectDrugBinding binding ;
    List<DrugBean.ResultEntity.ResEntity> strs = new ArrayList<>();
    List<DrugBean.ResultEntity.ResEntity> results = new ArrayList<>();
    DrugAdapter adapter;
    OnDrugSelectListener listener;

    public SelectDrugDialog(Context context,OnDrugSelectListener listener) {
        super(context);
        this.listener = listener;
        this.context = context;
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.dialog_select_drug,null,false);
        binding.setDialog(this);
        setContentView(binding.getRoot());
        initView();
    }

    private void initView() {
        strs.clear();
        getDrugList();
        adapter = new DrugAdapter(context,strs);
        binding.recycler.setLayoutManager(new LinearLayoutManager(context));
        binding.recycler.setAdapter(adapter);
        adapter.setOnItemClickLitener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (listener != null) {
                    listener.setOnDrugSelectListener(strs.get(position).getId(),
                            strs.get(position).getName());
                    strs.clear();
                    dismiss();
                }
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
    }

    public void getDrugList(){
        HTTPAPI.getInstance().getDrupList(new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtil.e("获取药品列表成功"+ s);
                DrugBean bean = GsonUtil.GsonToBean(s,DrugBean.class);
                results = bean.getResult().getRes();
                strs.clear();
                strs.addAll(results);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                LogUtil.e("获取药品列表失败"+ e.getMessage());
            }
        });
    }

}

